package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.glink.common.contants.BaseCodeConstants;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.barriers.*;
import com.glink.manage.service.BarriersService;
import com.glink.manage.vo.barriers.BarriersFindVO;
import com.glink.manage.vo.barriers.BarriersTreeVO;
import com.glink.manage.vo.barriers.BarriersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/API/Manage/devices")
@Api(tags = "车辆和行人道闸管理")
public class BarriersController extends AbstractBaseController{

    @Autowired
    private BarriersService barriersService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "01.行人道闸信息分页查询", notes = "1.行人道闸信息分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/findBarriersPagePeople", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<BarriersVO>> findBarriersPagePeople(@RequestBody @Validated BarriersPageDTO barriersPageDTO) throws GeneralSimpleException {
        log.info("00.道闸信息分页查询:{}", barriersPageDTO );
        IPage<BarriersVO> barriersVOIPageList = barriersService.findBarriersPagePeople(barriersPageDTO);
        log.info("00.道闸信息分页查询: success");
        return this.response(barriersVOIPageList);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "02.车辆道闸信息分页查询", notes = "2.车辆道闸信息分页查询")
    @PostMapping(value = "/findBarriersPageCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<BarriersVO>> findBarriersPageCar(@RequestBody @Validated BarriersPageDTO barriersPageDTO) throws GeneralSimpleException {
        log.info("00.道闸信息分页查询:{}", barriersPageDTO );
        IPage<BarriersVO> barriersVOIPageList = barriersService.findBarriersPageCar(barriersPageDTO);
        log.info("00.道闸信息分页查询: success");
        return this.response(barriersVOIPageList);
    }


    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "03.行人新增设备", notes = "3.行人新增设备信息")
    @PostMapping(value = "/addPeople", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> addBarriersPeople(@RequestBody @Validated BarriersDTO barriersDTO) throws GeneralSimpleException {
        log.info("04.新增设备: {}", barriersDTO);
        boolean status = barriersService.saveBarriersPeople(barriersDTO);
        log.info("04.新增设备: success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "04.车辆新增设备", notes = "4.车辆新增设备信息")
    @PostMapping(value = "/addCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> addBarriersCar(@RequestBody @Validated BarriersDTO barriersDTO) throws GeneralSimpleException {
        log.info("04.新增设备: {}", barriersDTO);
        boolean status = barriersService.saveBarriersCar(barriersDTO);
        log.info("04.新增设备: success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "05.行人更新设备", notes = "5.行人更新设备信息")
    @PostMapping(value = "/updatePeople", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateBarriersPeople(@RequestBody @Validated BarriersUpdateDTO barriersUpdateDTO) throws GeneralSimpleException {
        log.info("05.更新设备: {}", barriersUpdateDTO);
        boolean status = barriersService.updateBarriers(barriersUpdateDTO);
        log.info("05.更新设备: success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "06.车辆更新设备", notes = "6.车辆更新设备信息")
    @PostMapping(value = "/updateCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateBarriersCar(@RequestBody @Validated BarriersUpdateDTO barriersUpdateDTO) throws GeneralSimpleException {
        log.info("06.更新设备: {}", barriersUpdateDTO);
        boolean status = barriersService.updateBarriers(barriersUpdateDTO);
        log.info("06.更新设备: success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "07.行人删除设备", notes = "7.行人删除设备信息")
    @PostMapping(value = "/deletePeople", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> deleteBarriersPeople(@RequestBody List<String> objIdList) {
        log.info("07.删除设备: objIds={}", objIdList);
        boolean status = barriersService.deleteBarriers(objIdList);
        log.info("07.删除设备: success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "08.车辆删除设备", notes = "8.车辆删除设备信息")
    @PostMapping(value = "/deleteCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> deleteBarriersCar(@RequestBody List<String> objIdList) {
        log.info("08.删除设备: objIds={}", objIdList);
        boolean status = barriersService.deleteBarriers(objIdList);
        log.info("08.删除设备: success");
        return this.response(status);
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "09.行人导出设备数据", notes = "9.行人导出设备信息为Excel文件")
    @PostMapping(value = "/exportPeople", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> exportBarriersDataPeople(@RequestBody @Validated BarriersPageDTO barriersPageDTO) throws IOException {
        log.info("09.导出设备数据");
        ByteArrayOutputStream outputStream = barriersService.exportBarriersData(barriersPageDTO, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType02);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=barriers_info.xlsx");
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        log.info("09.导出设备数据: success");
        return ResponseEntity.ok()
                .headers(headers)
                .body(outputStream.toByteArray());
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "010.车辆导出设备数据", notes = "10.车辆导出设备信息为Excel文件")
    @PostMapping(value = "/exportCar", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public ResponseEntity<byte[]> exportBarriersDataCar(@RequestBody @Validated BarriersPageDTO barriersPageDTO) throws IOException {
        log.info("010.导出设备数据");
        ByteArrayOutputStream outputStream = barriersService.exportBarriersData(barriersPageDTO, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType01);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=barriers_info.xlsx");
        headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        log.info("010.导出设备数据: success");
        return ResponseEntity.ok()
                .headers(headers)
                .body(outputStream.toByteArray());
    }

    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "11.根据行人设备id查询详情", notes = "11.根据行人设备id查询详情")
    @PostMapping(value = "/findBarriersPeople", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<BarriersFindVO> findBarriersPeople(@RequestBody @Validated BarriersFindDTO barriersFindDTO) {
        log.info("11.根据行人设备id查询详情: objIds={}", barriersFindDTO);
        BarriersFindVO barriersFindVO = barriersService.findBarriers(barriersFindDTO, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType02);
        log.info("11.根据行人设备id查询详情: success");
        return this.response(barriersFindVO);
    }

    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "12.根据车辆设备id查询详情", notes = "12.根据车辆设备id查询详情")
    @PostMapping(value = "/findBarriersCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<BarriersFindVO> findBarriersCar(@RequestBody @Validated BarriersFindDTO barriersFindDTO) {
        log.info("12.根据车辆设备id查询详情: objIds={}", barriersFindDTO);
        BarriersFindVO barriersFindVO = barriersService.findBarriers(barriersFindDTO, BaseCodeConstants.CodeType_BarrierGateType.KEY_BarrierGateType01);
        log.info("12.根据车辆设备id查询详情: success");
        return this.response(barriersFindVO);
    }

    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "13.车辆和行人设备树形数据", notes = "13.车辆和行人设备树形数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/findTree", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<BarriersTreeVO>> findTre(@RequestBody @Validated BarriersTreeDTO barriersTreeDTO) throws GeneralSimpleException {
        log.info("13.车辆和行人设备树形数据：{}", barriersTreeDTO);
        List<BarriersTreeVO> barriersTreeVO = barriersService.findTree(barriersTreeDTO);
        return this.response(barriersTreeVO);
    }

}
